package com.zhaopin.utils;

import com.esotericsoftware.yamlbeans.YamlConfig;
import com.esotericsoftware.yamlbeans.YamlException;
import com.esotericsoftware.yamlbeans.YamlReader;
import com.esotericsoftware.yamlbeans.YamlWriter;
import com.zhaopin.bean.Contact;
import com.zhaopin.bean.Phone;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @create 2020-05-18 15:31
 */
public class YamlUtils {

    /**
     * 读取yaml 文件
     *
     * @throws YamlException
     * @throws FileNotFoundException
     */
    @Test
    public void testReadYaml() throws YamlException, FileNotFoundException {
        YamlReader reader = new YamlReader(new FileReader("dir/utils/yaml/contact.yml"));
        Object object = reader.read();
        System.out.println(object);
        Map map = (Map) object;
        System.out.println(map.get("address"));
    }


    /**
     * 读取多个对象
     *
     * @throws FileNotFoundException
     * @throws YamlException
     */
    @Test
    public void testReadMulti() throws FileNotFoundException, YamlException {
        YamlReader reader = new YamlReader(new FileReader("dir/utils/yaml/contactMulti.yml"));
        List contactList = (ArrayList) reader.read();
        for (int i = 0; i < contactList.size(); i++) {
            Map contact = (Map) contactList.get(i);
            if (contact == null) {
                break;
            }
            System.out.println(contact.get("age"));
        }

    }


    /**
     * 反序列化对象
     *
     * @throws FileNotFoundException
     * @throws YamlException
     */
    @Test
    public void testOneContact() throws FileNotFoundException, YamlException {
        YamlReader reader = new YamlReader(new FileReader("dir/utils/yaml/contactOne.yml"));
        Contact contact = reader.read(Contact.class);
        System.out.println(contact.getAge());
    }


    /**
     * 序列化对象
     */
    @Test
    public void testOutput() throws IOException {
        Contact contact = new Contact();
        contact.setName("Nathan Sweet");
        contact.setAge(28);
        YamlWriter writer = new YamlWriter(new FileWriter("dir/utils/yaml/output.yml"));
        writer.write(contact);
        writer.close();
    }

    /**
     * tag 截取
     */
    @Test
    public void testTag() throws IOException {
        Contact contact = new Contact();
        contact.setName("Nathan Sweet");
        contact.setAge(28);
        YamlWriter writer = new YamlWriter(new FileWriter("dir/utils/yaml/output.yml"));
        writer.getConfig().setClassTag("contact", Contact.class);
        writer.write(contact);
        writer.close();
    }

    /**
     * 书写复杂结构的yaml文件
     *
     * @throws IOException
     */
    @Test
    public void testWriteComplex() throws IOException {
        //前期数据准备
        List phoneNumbers = new ArrayList();
        Phone phone = new Phone();
        phone.setNumber("206-555-1234");
        Phone phone1 = new Phone();
        phone1.setNumber("206-555-5678");
        Phone phone2 = new Phone();
        phone2.setNumber("206-555-7654");
        phoneNumbers.add(phone);
        phoneNumbers.add(phone1);
        phoneNumbers.add(phone2);
        Contact contact = new Contact();
        contact.setName("Nathan Sweet");
        contact.setAge(28);
        contact.setPhoneNumbers(phoneNumbers);

        //数据书写
        YamlWriter writer = new YamlWriter(new FileWriter("dir/utils/yaml/output.yml"));
        writer.getConfig().setPropertyElementType(Contact.class, "phoneNumbers", Phone.class);
        writer.write(contact);
        writer.close();
    }

    /**
     * 不允许重复的 字段显示
     */
    @Test
    public void testError() {
        try {
            YamlConfig yamlConfig = new YamlConfig();
            // default value is true
            yamlConfig.setAllowDuplicates(false);
            YamlReader reader = new YamlReader(new FileReader("dir/utils/yaml/contacts.yml"), yamlConfig);
            Object object = reader.read();
            System.out.println(object);
            Map map = (Map) object;
            System.out.println(map.get("address"));
        } catch (YamlException | FileNotFoundException ex) {
            ex.printStackTrace();
            // or handle duplicate key case here according to your business logic
        }
    }


}
